package com.ruoyi.order.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.order.domain.SysOrder;
import com.ruoyi.order.domain.SysProd;
import com.ruoyi.order.mapper.SysOrderMapper;
import com.ruoyi.order.service.ISysOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 订单记录Service业务层处理
 * 
 * @author yj
 * @date 2023-11-14
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService
{
    @Resource
    private SysOrderMapper sysOrderMapper;



    /**
     * 查询订单记录
     * 
     * @param id 订单记录主键
     * @return 订单记录
     */
    @Override
//    public SysOrder selectSysOrderById(Long id,Integer receiptType)
    public SysOrder selectSysOrderById(Long id)
    {
//        return sysOrderMapper.selectSysOrderById(id,receiptType);
        return sysOrderMapper.selectSysOrderById(id);
    }

    /**
     * 查询未收货物品
     * @return
     */
    @Override
    public SysOrder noReceiptList(Long id) {
        return sysOrderMapper.noReceiptList(id);
    }

    /**
     * 查询订单记录列表
     * 
     * @param sysOrder 订单记录
     * @return 订单记录
     */
    @Override
    public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
    {
        return sysOrderMapper.selectSysOrderList(sysOrder);
    }

    public List<SysOrder> getOrderAndProd(){
        return sysOrderMapper.getOrderAndProd();
    }

    /**
     * 新增订单记录
     * 
     * @param sysOrder 订单记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysOrder(SysOrder sysOrder)
    {
        sysOrder.setCreateTime(DateUtils.getNowDate());
        sysOrder.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
//        int rows = sysOrderMapper.insertSysOrder(sysOrder);
//        if (sysOrderMapper.selectSysOrderByOrderCode(sysOrder.getOrderCode()) != 0) {
//            AjaxResult.error("编码重复");
//        }
//        insertSysProd(sysOrder);
        return sysOrderMapper.insertSysOrder(sysOrder);
    }

    /**
     * 修改订单记录
     * 
     * @param sysOrder 订单记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        sysOrder.setUpdateTime(DateUtils.getNowDate());
        sysOrder.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        sysOrderMapper.deleteSysProdByOrderCode(sysOrder.getOrderCode());
        insertSysProd(sysOrder);
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 批量删除订单记录
     * 
     * @param ids 需要删除的订单记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysOrderByIds(Long[] ids)
    {
        sysOrderMapper.deleteSysProdByOrderCodes(ids);
        return sysOrderMapper.deleteSysOrderByIds(ids);
    }

    /**
     * 删除订单记录信息
     * 
     * @param id 订单记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysOrderById(Long id)
    {
//        sysOrderMapper.deleteSysProdByOrderCode(sysOrderMapper.selectSysOrderById(id,null).getOrderCode());
        sysOrderMapper.deleteSysProdByOrderCode(sysOrderMapper.selectSysOrderById(id).getOrderCode());
        return sysOrderMapper.deleteSysOrderById(id);
    }

    @Override
    public int insertSysProd(SysProd sysProd) {
        return sysOrderMapper.insertSysProd(sysProd);
    }

    /**
     * 新增物品列信息
     * 
     * @param sysOrder 订单记录对象
     */
    public void insertSysProd(SysOrder sysOrder)
    {
        List<SysProd> sysProdList = sysOrder.getSysProdList();
        String orderCode = sysOrder.getOrderCode();
        if (StringUtils.isNotNull(sysProdList))
        {
            List<SysProd> list = new ArrayList<SysProd>();
            for (SysProd sysProd : sysProdList)
            {
                sysProd.setOrderCode(orderCode);
                list.add(sysProd);
            }
            if (list.size() > 0)
            {
                sysOrderMapper.batchSysProd(list);
            }
        }
    }
}
